var KEY_ENUM ={	
        KEY_PAGEUP:33,
		KEY_PAGEDOWN: 34,
		KEY_RETURN : 8,
		KEY_LEFT : 37,
		KEY_UP : 38,
		KEY_RIGHT:39,
		KEY_DOWN: 40,
		KEY_0:48,
		KEY_1:49,
		KEY_2:50,
		KEY_3:51,
		KEY_4:52,
		KEY_5:53,	
		KEY_6:54,
		KEY_7:55,	
		KEY_8:55,	
		KEY_9:57,
		KEY_L:108,
		KEY_R:114,
		KEY_U:117,
		KEY_D:100,
		KEY_Q:113,
		KEY_INFO:10,
		KEY_BACK:8,
		KEY_OK:13,
		KEY_44:44,
		KEY_46:46,
		KEY_OK:13,
		KEY_VOL_UP:259,
		KEY_VOL_DOWN:260,
		KEY_CHANNEL_UP:257,
		KEY_CHANNEL_DOWN:258,
		KEY_VOD_OVER:768
}
function log(msg){
	//$("#msgbox").html(msg);
	var msgCur = $("#msgbox").html();
	$("#msgbox").html(msgCur+msg);
}
var Class = {
        create: function() {
            var parent = null, properties = $A(arguments);
            if (Object.isFunction(properties[0]))
                parent = properties.shift();

            function klass() {
                this.initialize.apply(this, arguments);
            }

            Object.extend(klass, Class.Methods);
            klass.superclass = parent;
            klass.subclasses = [];

            if (parent) {
                var subclass = function() { };
                subclass.prototype = parent.prototype;
                klass.prototype = new subclass;
                parent.subclasses.push(klass);
            }

            for (var i = 0; i < properties.length; i++)
                klass.addMethods(properties[i]);

            if (!klass.prototype.initialize)
                klass.prototype.initialize = Prototype.emptyFunction;

            klass.prototype.constructor = klass;

            return klass;
        }
};

Class.Methods = {
        addMethods: function(source) {
            var ancestor = this.superclass && this.superclass.prototype;
            var properties = Object.keys(source);

            if (!Object.keys({ toString: true }).length)
                properties.push("toString", "valueOf");

            for (var i = 0, length = properties.length; i < length; i++) {
                var property = properties[i], value = source[property];
                if (ancestor && Object.isFunction(value) && value.argumentNames().first() == "$super") {
                    var method = value;
                    value = (function(m) {
                        return function() { return ancestor[m].apply(this, arguments) };
                    })(property).wrap(method);

                    value.valueOf = method.valueOf.bind(method);
                    value.toString = method.toString.bind(method);
                }
                this.prototype[property] = value;
            }

            return this;
        }
};

Object.extend = function(destination, source) {
        for (var property in source)
            destination[property] = source[property];
        return destination;
};

function $A(iterable) {
        if (!iterable) return [];
        if (iterable.toArray) return iterable.toArray();
        var length = iterable.length || 0, results = new Array(length);
        while (length--) results[length] = iterable[length];
        return results;
}



Object.extend(Object, {
            keys: function(object) {
                var keys = [];
                for (var property in object)
                    keys.push(property);
                return keys;
            },
            isFunction: function(object) {
                return typeof object == "function";
            },
            isUndefined: function(object) {
                return typeof object == "undefined";
            }
        });
Object.extend(Function.prototype, {
		argumentNames: function() {
			var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1].replace(/\s+/g, '').split(',');
			return names.length == 1 && !names[0] ? [] : names;
		},
		bind: function() {
			if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
			var __method = this, args = $A(arguments), object = args.shift();
			return function() {
				return __method.apply(object, args.concat($A(arguments)));
			}
		},
		wrap: function(wrapper) {
			var __method = this;
			return function() {
				return wrapper.apply(this, [__method.bind(this)].concat($A(arguments)));
			}
		}
});

Object.extend(Array.prototype, {
            first: function() {
                return this[0];
            }
});


var EpgEffect = function(id){
	var obj = document.getElementById(id);
	var objText = document.getElementById(id+"_text");
	var objMarquee = document.getElementById(id+"_marquee");

	this.hasClass=function (cls)  {  
	    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));  
	}  
	this.addClass  =function(cls){  
	    if (!this.hasClass(cls)) obj.className = cls+" "+obj.className;  
	    return this;
	}  
	  
	this.removeClass =function(cls) {  
	    if (this.hasClass( cls)) {  
	        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');  
	        obj.className = obj.className.replace(reg, ' ');  
	    }  
	    return this;
	}  
	this.toggleClass = function(cls){  
	    if(this.hasClass(obj,cls)){  
	        removeClass(obj, cls);  
	    }else{  
	        addClass(obj, cls);  
	    }  
	    return this;
	}  
	this.marquee =function(){
		if(objMarquee && objMarquee.innerText != ""){
			if(objText ){
				objText.style.display="none";
			}
			objMarquee.style.display="block";
		}
		return this;

	}
	this.marqueeStop =function(){
		if(objMarquee ){
			objMarquee.style.display="none";
			if(objText){	
				objText.style.display="block";
			}
		}	
		return this;
	}
	this.zoomIn =function(){
		return this;
	}
	this.zoomOut =function(){
		return this;
	}
	
	this.zoomOut =function(){
		return this;
	}
	this.cursor =function(src){
		obj.src= src;
	}
	
	
}
var EPG={
		 appendParam:function(url, paraName, paraValue) {
		    if (paraName == '') return url;
		    var updateUrl;
		    if (url.indexOf(paraName) == -1) {
		        if (url.indexOf("?") == -1)
		            updateUrl = url + '?' + paraName + '=' + paraValue;
		        else
		            updateUrl = url + '&' + paraName + '=' + paraValue;
		    }else{
		    	var reg=new RegExp("(\\\?|&)"+paraName+"=([^&]+)(&|$)","i");
		        var m = url.match(reg);
		    	if(m){
		    		updateUrl = url.replace(reg,function($0,$1,$2){return ($0.replace($2,paraValue));});
	            }else {
	            	updateUrl = url;
	            }
		    }
		    return updateUrl;
		},
		isChinese:function(temp)  {  
			 var re = /[^\u4e00-\u9fa5]/;  
			 if(re.test(temp)){
				 return false;  
			 }else{
				 return true;
			 }
		},
		subString:function(str,length,subfix){
			var result =""
			var idx = 0;
			for(var i=0;i<str.length;i++){
				var aChar = str.charAt(i);
				if(this.isChinese(aChar)){
					idx = idx+2;
					result=result+aChar;
				}else{
					idx++;
					result=result+aChar;
				}
				if(idx>=length*2){
					break;
				}
			}
			if((str!=result) && subfix){
				result = result + subfix; 
			}
			return result;
		},
		gotoPage:function(url,id){
			if(url=="${epgPageContext.backUrl}"){
				window.location.href = Authentication.CTCGetConfig('EPGDomain');
				return
			}
			if(id){
				window.location.href = this.appendParam(url,"lfid",id);
			}else{		
					window.location.href = url;
			}
		},
		gotoSelf:function(url,id){
			if(id){
				window.location.href = this.appendParam(url,"efid",id);
			}else{
				window.location.href = url;
			}
		}
		
}
